package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.Post;
import com.sxkf.template.entity.Product;
import com.sxkf.template.entity.ProductView;
import com.sxkf.template.entity.Store;
import com.sxkf.template.service.PostService;
import com.sxkf.template.service.ProductService;
import com.sxkf.template.service.ProductViewService;
import com.sxkf.template.service.StoreService;
import com.sxkf.template.vo.request.user.DeleteBatchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 查看足迹 前端控制器
 * </p>
 *
 * @author li
 * @since 2024-12-11 18:30:40
 */
@Api(tags = "足迹")
@RestController
@RequestMapping("/productView")
public class ProductViewController {
    @Resource
    private ProductViewService productViewService;
    @Resource
    private ProductService productService;
    @Resource
    private StoreService storeService;
    @Resource
    private PostService postService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<IPage<Object>> page(@RequestParam Integer pageNo,
                                  @RequestParam Integer pageSize,
                                  @ApiParam("查看类型(1=商品 2=店铺 3=互动 4=动态)") @RequestParam String viewType,
                                 @RequestParam String userId){
        Page<Object> objectPage = new Page<>(pageNo,pageSize);
        Page<ProductView> page = productViewService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<ProductView>()
                .eq(ProductView::getViewType, viewType)
                .eq(ProductView::getUserId, userId));

        List<String> idList = page.getRecords()
                .stream()
                .map(ProductView::getViewId)
                .collect(Collectors.toList());
        if (idList.isEmpty()){
            return R.success(objectPage);
        }
        ArrayList<Object> list = new ArrayList<>();
        switch (viewType){
            case "1":
                List<Product> productList = productService.listByIds(idList);
                BeanUtils.copyProperties(page, objectPage);
                list.addAll(productList);
                objectPage.setRecords(list);
                break;
            case "2":
                List<Store> storeList = storeService.listByIds(idList);
                BeanUtils.copyProperties(page, objectPage);
//                objectPage.setRecords(Collections.singletonList(storeList));
                list.addAll(storeList);
                objectPage.setRecords(list);
                break;
            case "3":
            case "4":
                List<Post> postList = postService.list(new LambdaQueryWrapper<Post>().eq(Post::getType,viewType).in(Post::getId,idList));
                BeanUtils.copyProperties(page, objectPage);
//                objectPage.setRecords(Collections.singletonList(postList));
                list.addAll(postList);
                objectPage.setRecords(list);
                break;
            default:break;
        }
        return  R.success(objectPage);
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/deleteBatch")
    public R<?> deleteBatch(@RequestBody DeleteBatchVO deleteBatchVO){
        productViewService.remove(new LambdaQueryWrapper<ProductView>()
                .eq(ProductView::getUserId,deleteBatchVO.getUserId())
                .eq(ProductView::getViewType,deleteBatchVO.getType())
                .in(ProductView::getViewId,deleteBatchVO.getTargetIds())
        );
        return R.success();
    }
}
